home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / std / c++ / 591 < prev    next >
Encoding:
Internet Message Format  |  1996-08-06  |  2.6 KB

  1. From: phalpern@truffle.ultranet.com (Pablo Halpern)
  2. Message-ID: <4h24c2$pq@caesar.ultra.net>
  3. X-Original-Date: Wed, 28 Feb 1996 17:45:33 GMT
  4. Path: in1.uu.net!bounce-back
  5. Date: 29 Feb 96 06:15:51 GMT
  6. Approved: fjh@cs.mu.oz.au
  7. Newsgroups: comp.std.c++
  8. Subject: Re: operators new[]/delete[]
  9. Organization: UltraNet Communications, Inc.
  10. References: <313166CF.11C2@orbotech.co.il> <4gu414$62u@mulga.cs.mu.OZ.AU>
  11. X-Newsreader: Forte Agent .99b.113
  12. X-Auth: PGPMoose V1.1 PGP comp.std.c++
  13.     iQBFAgUBMTVExOEDnX0m9pzZAQFKjAF/VI9a6LQ4v/MLK8RoeJuDxkGrM/0iQZ1I
  14.     MdQ4uhevCWQeA5oJ7y3YpclVPbQmyjoL
  15.     =jGxQ
  16.  
  17. Although I don't agree with most of what Constantine Antonovich says, I
  18. do wonder about one particular set of situations. Many allocation and
  19. re-allocation systems use the default new() operator to allocate memory
  20. in the form of char arrays. This "raw" memory is then recast to an array
  21. of specific type, which is initialized one element at a time:
  22.  
  23.   template <class T>
  24.   T* dup_array(const T* p, size_t s)
  25.   {
  26.     T *p2 = reinterpret_cast<T*> new char[s * sizeof(T)];  // note 1
  27.     while (s-- > 0)
  28.       new (p2 + s) T(p[s]);  // Initialize using copy constructor
  29.   }
  30.  
  31.   void f(T* p, size_t s)
  32.   {
  33.     T* newp = dup_array(p, s);
  34.     // do something with newp
  35.     delete [] newp;     // note 2
  36.   }
  37.  
  38. I believe that something similar to the line marked "note 1" is common
  39. practice for this sort of operation. However, I believe that the line
  40. marked "note 2" is undefined behavior. Is there a way in the standard
  41. can be modified so that the above code becomes well-defined and works as
  42. intended? How does the STL deal with this in its "unitialized copy"
  43. operation?
  44.  
  45. There is another way to allocate raw memory, but here the operations are
  46. even less defined (I believe):
  47.  
  48.    void *p2 = operator new[] (s * sizeof(T));
  49.    delete p2;  // What does this do? p2 was not the result of a normal
  50.                // new expression.
  51.    delete reinterpret_cast<T*> p2;  // What does this do?
  52.  
  53. How do we solve these problems in a standard-conforming way. Does there
  54. need to be special language for char arrays or void * allocations. (Or
  55. is there already? I haven't found it.)
  56.  
  57. Thanks,
  58.       
  59. Pablo Halpern                   phalpern@truffle.ultranet.com
  60.  
  61. I am self-employed. Therefore, my opinions *do* represent 
  62. those of my employer.
  63. ---
  64. [ To submit articles: try just posting with your news-reader.
  65.                       If that fails, use mailto:std-c++@ncar.ucar.edu
  66.   FAQ:      http://reality.sgi.com/employees/austern_mti/std-c++/faq.html
  67.   Policy:   http://reality.sgi.com/employees/austern_mti/std-c++/policy.html
  68.   Comments? mailto:std-c++-request@ncar.ucar.edu.
  69. ]
  70.